<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Skit;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Skit\Ctl\SkitTplRmdCtl;
use Srv\Apps\Proto\DTO\Skit\SkitTplRmdAddDTO;
use Srv\Apps\Proto\DTO\Skit\SkitTplRmdDelDTO;
use Srv\Apps\Proto\DTO\Skit\SkitTplRmdEditDTO;
use Srv\Apps\Proto\DTO\Skit\SkitTplRmdListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Skit\SkitTplRmdListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class SkitTplRmdAct extends ActionAbstract
{
    /**
     * @url /skit/tpl/rmd/list
     * @auth skitTplRmdList
     * @req Srv.Apps.Proto.DTO.Skit.SkitTplRmdListDTO
     * @rsp Srv.Apps.Proto.VO.Skit.SkitTplRmdListVO
     * 推荐短剧模板列表
     */
    public function getSkitTplRmdList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof SkitTplRmdListDTO) || !($VOClass instanceof SkitTplRmdListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = SkitTplRmdCtl::tag()->getSkitTplRmdList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /skit/tpl/rmd/add
     * @auth skitTplRmdAdd
     * @req Srv.Apps.Proto.DTO.Skit.SkitTplRmdAddDTO
     * 推荐短剧模板新增
     */
    public function skitTplRmdAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitTplRmdAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl         = LogCtl::tag();
        $logData        = $setData;
        $tprdId         = SkitTplRmdCtl::tag()->skitTplRmdAdd($setData);
        if($tprdId < 1) {
            $errMsg     = '推荐短剧模板新增失败';
            $LogCtl->logAdd(LogCtl::SKIT_TPL_RMD_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '推荐短剧模板新增成功';
        $LogCtl->logAdd(LogCtl::SKIT_TPL_RMD_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /skit/tpl/rmd/edit
     * @auth skitTplRmdEdit
     * @req Srv.Apps.Proto.DTO.Skit.SkitTplRmdEditDTO
     * 推荐短剧模板编辑
     */
    public function skitTplRmdEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitTplRmdEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $tprdId         = intval($DTOClass->getTprdId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl         = LogCtl::tag();
        $SkitTheaterCtl = SkitTplRmdCtl::tag();
        $tplRmdSrc      = $SkitTheaterCtl->getSkitTplRmdSrc($tprdId);
        if(!isset($tplRmdSrc['tprd_id'])){
            $logData    = $setData;
            $errMsg     = '推荐短剧模板不存在';
            $LogCtl->logAdd(LogCtl::SKIT_TPL_RMD_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result         = $SkitTheaterCtl->skitTplRmdEdit($tprdId, $setData, $tplRmdSrc);
        $logData        = ['new' => $setData, 'old' => $tplRmdSrc];
        if(!$result) {
            $errMsg     = '推荐短剧模板编辑失败';
            $LogCtl->logAdd(LogCtl::SKIT_TPL_RMD_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '推荐短剧模板编辑成功';
        $LogCtl->logAdd(LogCtl::SKIT_TPL_RMD_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /skit/tpl/rmd/del
     * @auth skitTplRmdDel
     * @req Srv.Apps.Proto.DTO.Skit.SkitTplRmdDelDTO
     * 推荐短剧模板删除
     */
    public function skitTplRmdDel(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof SkitTplRmdDelDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $tprdId         = intval($DTOClass->getTprdId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $logData        = $setData;
        $LogCtl         = LogCtl::tag();
        $SkitTheaterCtl = SkitTplRmdCtl::tag();
        $tplRmdSrc      = $SkitTheaterCtl->getSkitTplRmdSrc($tprdId);
        if(!isset($tplRmdSrc['tprd_id'])){
            $errMsg     = '推荐短剧模板不存在';
            $LogCtl->logAdd(LogCtl::SKIT_TPL_RMD_DEL, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result         = $SkitTheaterCtl->skitTplRmdDel($tprdId);
        if(!$result){
            $errMsg     = '推荐短剧模板删除失败';
            $LogCtl->logAdd(LogCtl::SKIT_TPL_RMD_DEL, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '推荐短剧模板删除成功';
        $LogCtl->logAdd(LogCtl::SKIT_TPL_RMD_DEL, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}